package com.uam.core.dao.mysql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.uam.core.entity.model.SectorStatModel;

import lombok.extern.slf4j.Slf4j;

@Repository
@Slf4j
public class SectorStatDao {

	@Autowired
	private JdbcTemplate jdbcTemplate = null;

	private boolean sectorStatIsExist(String id) {
		return this.jdbcTemplate.query(con -> {
			PreparedStatement ps = con.prepareStatement("SELECT `ID` FROM `SECTOR_STAT` WHERE `ID`=?");
			ps.setString(1, id);
			return ps;
		}, new ResultSetExtractor<Boolean>() {
			public Boolean extractData(ResultSet resultSet) throws SQLException, DataAccessException {
				return Boolean.valueOf(resultSet.next());
			}
		}).booleanValue();
	}

	public boolean saveSectorStat(SectorStatModel sectorStat) {
//		SectorStatDao.log.info("---------保存扇区统计，params:{}---------", sectorStat);
		String id = sectorStat.getId();
		if (!this.sectorStatIsExist(id)) {
			return this.jdbcTemplate.update(con -> {
				PreparedStatement ps = con.prepareStatement(
						"INSERT INTO `SECTOR_STAT`(`ID`,`NAME`,`NUM`,`AIRCRAFT_NAMES`,`TIME`) VALUES (?,?,?,?,?)");
				ps.setString(1, id);
				ps.setString(2, sectorStat.getName());
				ps.setInt(3, sectorStat.getNum());
				ps.setString(4, sectorStat.getAircraftNames());
				ps.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
				return ps;
			}) == 1;
		}
		return this.jdbcTemplate.update(con -> {
			PreparedStatement ps = con.prepareStatement(
					"UPDATE `SECTOR_STAT` SET `NAME`=?,`NUM`=?,`AIRCRAFT_NAMES`=?,`TIME`=? WHERE `ID`=?");
			ps.setString(1, sectorStat.getName());
			ps.setInt(2, sectorStat.getNum());
			ps.setString(3, sectorStat.getAircraftNames());
			ps.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
			ps.setString(5, id);
			return ps;
		}) == 1;
	}

	public boolean updateSectorStat(SectorStatModel sectorStat) {
		return this.jdbcTemplate.update(con -> {
			PreparedStatement ps = con.prepareStatement(
					"UPDATE `SECTOR_STAT` SET `NAME`=?,`NUM`=?,`AIRCRAFT_NAMES`=?,`TIME`=? WHERE `ID`=?");
			ps.setString(1, sectorStat.getName());
			ps.setInt(2, sectorStat.getNum());
			ps.setString(3, sectorStat.getAircraftNames());
			ps.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
			ps.setString(5, sectorStat.getId());
			return ps;
		}) == 1;
	}

	public boolean saveSectorCapacity(SectorStatModel sectorStat) {
//		SectorStatDao.log.info("---------保存扇区容量统计，params:{}---------", sectorStat);
		String id = sectorStat.getId();
		if (!this.sectorStatIsExist(id)) {
			return this.jdbcTemplate.update(con -> {
				PreparedStatement ps = con.prepareStatement(
						"INSERT INTO `SECTOR_STAT`(`ID`,`NAME`,`CAPACITY`,`TIME`) VALUES (?,?,?,?)");
				ps.setString(1, id);
				ps.setString(2, sectorStat.getName());
				ps.setInt(3, sectorStat.getCapacity());
				ps.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
				return ps;
			}) == 1;
		}
		return this.jdbcTemplate.update(con -> {
			PreparedStatement ps = con
					.prepareStatement("UPDATE `SECTOR_STAT` SET `NAME`=?,`CAPACITY`=?,`TIME`=? WHERE `ID`=?");
			ps.setString(1, sectorStat.getName());
			ps.setInt(2, sectorStat.getCapacity());
			ps.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
			ps.setString(4, id);
			return ps;
		}) == 1;
	}

}
